<?php
/*
 * transfert/load-index.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
?>
<?php
// Variables
$transferts	 = $this->transferts;
$type		 = (int) $this->type; // 1 : TE, 2 : TS
?>
<div class='list-container'>
    <table class='table table-striped list-table'>
	<thead>
	    <tr>
		<td width='10%'>Matricule</td>
		<td width='10%'>Prénom</td>
		<td width='10%'>Site <?php echo ($type == 1) ? 'de Provenance' : 'de Destination' ?></td>
		<td width='10%'>Date</td>
		<td width='10%'>Entrée</td>
		<td width='10%'>Sortie</td>
		<td width='30%'>Commentaire</td>
		<td>&nbsp;</td>
	    </tr>
	</thead>
	<tbody>
	    <?php if ($transferts->count() > 0): ?>
	    <!-- Il y a des transferts -->
		<?php foreach ($transferts as $t): ?>
		    <?php
		    $employe = $t->getEmploye();
		    $site	 = null;
		    if ($type == 1) {
			$site = $t->getSiteDepart();
		    }
		    else {
			$site = $t->getSiteArrivee();
		    }
		    $jour	 = new RHDateTime($t->journee);
		    $entree	 = new RHDateTime();
		    $entree->setTime($t->entree);
		    $sortie	 = new RHDateTime();
		    $sortie->setTime($t->sortie);
		    ?>
			    <tr>
			<td><?php echo $t->matricule; ?></td> 
			<td><?php echo $employe->prenom; ?></td>
			<td><?php echo $site->libelle; ?></td>
			<td><?php echo $jour->dateFr(); ?></td>
			<td><?php echo $entree->hours(); ?></td>
			<td><?php echo $sortie->hours(); ?></td>
			<td><?php echo $t->commentaire; ?></td>
			<td>
			    <a class='cancel-transfert' data-id='<?php echo $t->id; ?>' href='<?php echo $this->url('RHX/default', array('controller'	 => 'transfert', 'action'	 => 'cancel')); ?>'>
					<img src='<?php echo $this->basePath() . '/img/rhx/delete-16x16.png' ?>' alt='Effacer' title='Effacer'>
			    </a>
			</td>
		    </tr>
		<?php endforeach; ?>
	    <?php else: ?>
    	    <!-- Transfert Vide -->
    	    <tr>
    		<td colspan='8'>Il n'y pas encore de transfert</td>
    	    </tr>
	    <?php endif; ?>
	</tbody>
    </table>
</div>

<script type="text/javascript">
    // Démarrage
    $(function(){
	// Annuler un transfert
	$('.cancel-transfert').click(function(e){
	    e.preventDefault();
	    conf = confirm("Voulez-vous annuler le transfert ?");
	    if(!conf){
		return false;
	    }
	    ajax = $.ajax({
		dataType: 'json',
		type: 'post',
		url : $(this).attr('href'),
		data: '&id=' + $(this).attr('data-id')
	    });
	    ajax.done(function(data){
		refresh();
	    });
	});
    });
</script>